//
//  AdispatchView.m
//  wtshoppingcite
//
//  Created by TW on 14-11-7.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "AdispatchView.h"

@implementation AdispatchView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
//        self.backgroundColor = UIColorFromRGB(0x9fa0a0);
        self.alpha = 1;
        [self createSubView];
    }
    return self;
}

-(void)createSubView{
    
    _chooseTableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    _chooseTableView.dataSource = self;
    _chooseTableView.delegate = self;
    _chooseTableView.backgroundColor = [UIColor whiteColor];
    _chooseTableView.alpha = 1;
    _chooseTableView.scrollEnabled = NO;
    [_chooseTableView.layer setCornerRadius:5];
    _chooseTableView.clipsToBounds = YES;
    [self addSubview:_chooseTableView];
    
   
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _titleArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentitfer"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentitfer"];
        
        cell.textLabel.text =_titleArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:16.0f];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = UIColorFromRGB(0x9fa0a0);
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self dismissView];
    [_button setTitle:_titleArray[indexPath.row] forState:UIControlStateNormal];
    if (self.delegate && [self.delegate respondsToSelector:@selector(adispathView:)]) {
         [self.delegate adispathView:_titleArray[indexPath.row]];
    }
    
 
}


-(void)showView:(UIView*)superView{
    
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _backView.backgroundColor = Color(200, 200, 200, 0.4);
    [superView addSubview:_backView];
    
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
    [_backView addGestureRecognizer:tap];
    
    [superView bringSubviewToFront:self];
    
//    dispatch_async(dispatch_get_main_queue(), ^{
    self.top = -SCREEN_HEIGHT;
        [UIView animateWithDuration:0.4 animations:^{
            self.alpha = 1.0f;
//            self.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0,95);
            self.top = 95;
        }];
//    });
}

-(void)tapView:(UITapGestureRecognizer*)tap{
    [self dismissView];
}

-(void)dismissView{
    
    [_backView removeFromSuperview];
    _backView = nil;
    
    [UIView animateWithDuration:0.4 animations:^{
        self.alpha = 0.0f;
        self.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
    }];
}

@end
